package aex.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.Therapy;
import aex.service.ArticleCategoryService;
import aex.service.TherapyService;
import aex.util.CU;
import aex.util.PageBean;

import com.opensymphony.xwork2.ModelDriven;

@Controller("TherapyManagerAction")
@Scope("prototype")
public class TherapyManagerAction extends BaseActon implements ModelDriven<Therapy> {

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;
	
	@Resource(name = TherapyService.SERVICE_NAME)
	TherapyService therapyService;

	
	
	Therapy therapy = new Therapy();

	public TherapyManagerAction() {
		CU.deBugPrintln("TherapyManagerAction 被创建");
	}

	@Override
	public Therapy getModel() {
		return therapy;
	}


	public String therapyList(){
		String id=request.getParameter("id");
		PageBean<Therapy> pageBean = new PageBean<Therapy>();
		PageBean.initPageBean(pageBean, request,"&id="+id);
		pageBean = therapyService.findAllByCategory(pageBean,id);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("category", acService.findByID(id));
		return "therapyList";
	}
	

	/**
	 * 删除单个栏目
	 * @return
	 */
	public String deleteTherapy(){
		int id=Integer.parseInt(request.getParameter("id"));
		therapyService.deleteById(id);
		ajaxPrint("删除成功！");
		return null;
	}
	
	/**
	 * 删除多个栏目
	 * @return
	 */
	public String deleteTherapys(){
		String[] ids=request.getParameter("ids").split(",");
		for (int i = 0; i < ids.length; i++) {
			therapyService.deleteById(ids[i]);
		}
		ajaxPrint("成功删除"+ids.length+"条数据！");
		return null;
	}
	
	
	public String goAddTherapy(){
		String id=request.getParameter("id");
		request.setAttribute("category", acService.findByID(id));
		return "addTherapy";
	}
	
	public String addTherapy(){
		String id=request.getParameter("id");
		therapy.setArticleCategory(acService.findByID(id));
		therapy.setThOrderBy(therapyService.findTherapyCount() + 1);
		therapyService.save(therapy);
		return CU.goMsgPage(request, "成功添加一个疗法！", "ok", "manage/therapyManager_therapyList.do?id="+id);
	}
	
	public String editorTherapy(){
		request.setAttribute("therapy", therapyService.findByID(request.getParameter("id")));
		request.setAttribute("category", acService.findByID(request.getParameter("acId")));
		return "editorTherapy";
	}
	
	public String updateTherapy(){
		String id=request.getParameter("acId");
		Therapy oldTherapy= therapyService.findByID(therapy.getThId());
		therapy.setThOrderBy(oldTherapy.getThOrderBy());
		therapy.setArticleCategory(oldTherapy.getArticleCategory());
		therapyService.update(therapy);
		return CU.goMsgPage(request, "疗法信息已经更新！", "ok", "manage/therapyManager_therapyList.do?id="+id);
	}
	
	public String orderByTherapy(){
		String id=request.getParameter("id");
		request.setAttribute("seamCates", therapyService.findAllByCategoryNoPage(id));
		request.setAttribute("id", id);
		return "orderByTherapy";
	}
	

	
	public String updateTherapyOrderBy(){
		
		try{
			String[] seq=request.getParameter("cateSeq").split(",");
			//重新排序
			String id=request.getParameter("id");
			List<Therapy> seamCateList=therapyService.findAllByCategoryNoPage(id);
			for (int i = 0; i < seq.length; i++) {
				int newSeq=Integer.parseInt(seq[i]);
				if(seamCateList.get(i).getThOrderBy()!=newSeq){
					CU.deBugPrintln("更新"+seamCateList.get(i).getThName()+" "+seamCateList.get(i).getThOrderBy()+"---->"+newSeq);
					seamCateList.get(i).setThOrderBy(newSeq);
					therapyService.update(seamCateList.get(i));
				}
			}
			response.getWriter().write("{\"info\":\"修改成功！\",\"status\":\"y\"}");

		} catch (IOException e) {
			try {
				response.getWriter().write(
						"{\"info\":\"修改失败\",\"status\":\"y\"}");
			} catch (IOException e1) {
			}
		}
		return null;
	}
	
	


}
